﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ExpLib.Tools
{
    /// <summary>
    /// 根据当前的时间和内存中的序列号生成唯一的序列号类
    /// </summary>
    internal class UniqueStringID : IIDGenerator
    {
        const int MAXSEED = 9999;
        private static Object lockObj = new object();
        private static int seed = 0;


        private int GetSeed
        {
            get
            {
                lock (lockObj)
                {
                    seed = seed < MAXSEED ? seed : 0;

                    return seed;
                }
            }
        }

        /// <summary>
        /// 生成一个新的ID 形式如下年月日时分秒种子 如 100621350123
        /// </summary>
        /// <returns></returns>
        public string NewID()
        {
            return DateTime.Now.ToString("yyMMddHHmmss") + GetSeed.ToString().PadLeft(4, '0');
        }

        /// <summary>
        /// 生成一个新的ID 形式如下年月日时分秒种子 如 前缀+100621350123
        /// </summary>
        /// <returns></returns>
        public string NewID(string perfix)
        {
            return perfix + DateTime.Now.ToString("yyMMddHHmmss") + GetSeed.ToString().PadLeft(4, '0');
        }
    }
}